package com.example.service;

import cn.hutool.core.date.DateUtil;
import com.example.entity.ClassComment;
import com.example.mapper.ClassCommentMapper;
import com.example.mapper.CourseMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 管理员业务处理
 **/
@Service
public class ClassCommentService {

    @Resource
    private ClassCommentMapper classcommentMapper;
    @Resource
    private CourseMapper classMapper;
    /**
     * 新增
     */
    public void add(ClassComment classcomment) {
        classcomment.setTime(DateUtil.now());
        classcommentMapper.insert(classcomment);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        classcommentMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            classcommentMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(ClassComment classcomment) {
        classcommentMapper.updateById(classcomment);
    }

    /**
     * 根据ID查询
     */
    public ClassComment selectById(Integer id) {
        return classcommentMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<ClassComment> selectAll(ClassComment classcomment) {
        classcomment.setParentId(0);
        List<ClassComment> allParent = classcommentMapper.selectAll(classcomment);
        for (ClassComment dbClassComment : allParent) {
            classcomment.setParentId(dbClassComment.getId());
            List<ClassComment> children = classcommentMapper.selectAll(classcomment);
            dbClassComment.setChildren(children);
        }
        return allParent;
    }

    /**
     * 分页查询
     */
    public PageInfo<ClassComment> selectPage(ClassComment classcomment, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ClassComment> list = classcommentMapper.selectAll(classcomment);
        return PageInfo.of(list);
    }
}